gh-148680: Replace internal names with type_reprs of objects in string representations of ForwardRef#148682
Open
DavidCEllis wants to merge 9 commits intopython:mainfrom
Open
gh-148680: Replace internal names with type_reprs of objects in string representations of ForwardRef#148682DavidCEllis wants to merge 9 commits intopython:mainfrom
DavidCEllis wants to merge 9 commits intopython:mainfrom
Conversation
| resolved_str = type_repr(name_obj) | ||
| else: | ||
| visitor = _ExtraNameFixer(names) | ||
| ast_expr = ast.parse(resolved_str, mode="eval").body |
Member
There was a problem hiding this comment.
We might need to cache this, it's probably pretty slow. What do you think?
Contributor
Author
There was a problem hiding this comment.
This is in fact, not very fast. Definitely worth caching if it's going to be accessed frequently.
Contributor
Author
There was a problem hiding this comment.
>>> a_anno
ForwardRef('unknown | str | int | list[str] | tuple[int, ...]', is_class=True, owner=<class '__main__.Example'>)
>>> b_anno
ForwardRef('unknown', is_class=True, owner=<class '__main__.Example'>)
>>> a = timeit(lambda: a_anno.__resolved_forward_str__, number=10_000)
>>> b = timeit(lambda: b_anno.__resolved_forward_str__, number=10_000)
>>> a
0.18359258100008446
>>> b
0.0018204959997092374
>>> a / b
100.8475607907994
Contributor
Author
There was a problem hiding this comment.
I've added a cache and ended up shortening the name of the property and the cache. Adding the cache did mean adding an extra slot to both classes.
Not completely sold on the names I have if you have something better.
ashm-dev
reviewed
Apr 20, 2026
Fix a misplaced parenthesis that incorrectly caused a statement to evaluate as truthy Co-authored-by: Shamil <ashm.tech@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a new
ForwardRef.__resolved_forward_str__attribute which takes the__forward_arg__and replaces any internal__annotationlib_name_x__names with thetype_reprof the object they represent.Before:
After:
This approach replaces the extra_names with their
type_reprwhen the forwardref is evaluated to a string. I'm fairly sure that trying to get the original names as they are in the source would be both slower and significantly more complicated.This logic is largely based on something I already have for my
reannotatelibrary.